import 'package:flutter/material.dart';
import 'package:intro_views_flutter/intro_views_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'package:fluro/fluro.dart';
import 'package:flutter/rendering.dart';
import './routers/routes.dart';
import './routers/application.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import './config/service_method.dart';
import 'package:notification_permissions/notification_permissions.dart';
// import 'package:camera/camera.dart';
import './common/intro_slider.dart';
import './common/j_push.dart';
import './common/first_screen.dart';
import './common/get_CheckNotification.dart';
import 'package:oktoast/oktoast.dart';

import './pages/login.dart';

// List<CameraDescription> cameras;
void main()async{
    //SharedPreferences初始化没数据会报错，加这句
    SharedPreferences.setMockInitialValues({});

    //注册拦截器
    DioUtil.tokenInter();

    // cameras = await availableCameras();

    // 自定义错误的页面
    setCustomErrorPage();
    
    runApp(OKToast(
        child: MaterialApp(
            debugShowCheckedModeBanner: false,
            title: '签到',
            theme: ThemeData(
                platform: TargetPlatform.iOS,
                primaryColor: Color.fromRGBO(81, 126, 219, 1)
            ),
            home: StartApp()
        ),
    ));

        //  runApp(MaterialApp(
        //     debugShowCheckedModeBanner: false,
        //     title: '签到',
        //     theme: ThemeData(
        //         platform: TargetPlatform.iOS,
        //         primaryColor: Color.fromRGBO(81, 126, 219, 1)
        //     ),
        //     home: StartApp()
        // ));

    // 状态栏透明
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}

void setCustomErrorPage(){
    ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails){
        return Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
                color: Colors.white
            ),
            child:SingleChildScrollView(
                child: Column(
                    children: <Widget>[
                        Text(
                            "Flutter出错了",
                            style: TextStyle(
                                fontSize: 14.0
                            ),
                        ),
                        Text(
                            flutterErrorDetails.toString(),
                            style: TextStyle(
                                fontSize: 14.0
                            ),
                        ),
                    ],
                ),
            )
            
        );
    };
}

class StartApp extends StatefulWidget{
    @override
    _StartAppState createState() => _StartAppState();
}

class _StartAppState extends State<StartApp>with WidgetsBindingObserver{

    var loginState = 2;
    bool firstStartApp = false;
    
    @override
    void initState(){
        super.initState();
        // _firstStartApp();
        // _validateLogin();                               // 获取登陆状态
        // WidgetsBinding.instance.addObserver(this);      // 请求权限的时候在切换到app的时候执行didChangeAppLifecycleState
        // JPushInit();                                    //极光插件平台初始化
        final router = Router();                        //路由初始化
        Routes.configureRoutes(router);
        Application.router = router;
       
    }
    // @override
    // void didChangeAppLifecycleState(AppLifecycleState state) {
    //     if (state == AppLifecycleState.resumed) {
    //         GetCheckNotification.getCheckNotificationPermStatus(context);
    //     }
    // }

    
    @override
    Widget build(BuildContext context){
        ScreenUtil.init(context, width: 750, height: 1334);
        // _writeStartApp();
        
        return FirstScreen("image/first.jpg","/login");
        
        // if(firstStartApp == true){
        //     return IntroSlider("/login");
        // }else{
        //     if(loginState == 1){
        //         return FirstScreen("image/first.jpg","/home");
        //     }else{
        //         return FirstScreen("image/first.jpg","/login");
        //     }
        // }
    }

    // @override
    // void deactivate(){
    //     // 在build构建完成后显示获取权限窗口，
    //     GetCheckNotification.getCheckNotificationPermStatus(context);
    //     // print("deactivate---------------");
    // }


    _firstStartApp() {
            Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("firstStartApp");
            });
            future.then((val){
                if(val == null){
                    setState(() {
                        firstStartApp = true;
                    });
                }else{
                    setState(() {
                        firstStartApp = false;
                    });
                }
            }).catchError((_){
                print("catchError");
            });
    }
    _writeStartApp()async{
        SharedPreferences prefs =await SharedPreferences.getInstance();
        prefs.setString("firstStartApp","false");
    }

    _validateLogin() {
            Future<dynamic> future = Future(()async{
                SharedPreferences prefs =await SharedPreferences.getInstance();
                return prefs.getString("loginToken");
            });
            future.then((val){
                if(val == null){
                    setState(() {
                        loginState = 0;
                    });
                }else{
                    setState(() {
                        loginState = 1;
                    });
                }
            }).catchError((_){
                print("catchError");
            });
    }

}